package com.javabasic.algorithm.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/10/13 11:31 下午
 * @see
 */
public class BinaryTreePostorderTraversal {

    /**
     * 递归解法，遍历二叉树的后序遍历
     * @param root
     * @return
     */
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        getBinaryTreePostOrder(root,result);
        return result;
    }

    private void getBinaryTreePostOrder(TreeNode root, List<Integer> result) {
        if (root == null) return;
        getBinaryTreePostOrder(root.left, result);
        getBinaryTreePostOrder(root.right, result);
        result.add(root.val);
    }
}
